package com.key.oa.activity;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.key.keyleaderoa.R;
import com.key.oa.util.SysParams;
import com.key.oa.util.Util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class SettingActivity extends Activity implements OnClickListener {

	private ImageButton sureButton = null;
	
	private EditText ip1 = null;
	private EditText ip2 = null;
	private EditText ip3 = null;
	private EditText ip4 = null;
	private EditText port = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.key_setting_menu);

        if( SysParams.settingActivity != null ) {
        	SysParams.settingActivity.finish();
        }
        SysParams.settingActivity = this;
        
        initEditText();
        initSureButton();
        readServerFile();

    }
	
	private void initEditText() {
		ip1 = (EditText) findViewById(R.id.ip1);
		ip2 = (EditText) findViewById(R.id.ip2);
		ip3 = (EditText) findViewById(R.id.ip3);
		ip4 = (EditText) findViewById(R.id.ip4);
		port = (EditText) findViewById(R.id.port);
	}
	
	private void initSureButton() {
		
		sureButton = (ImageButton) findViewById(R.id.sureButton);
		sureButton.setOnClickListener(this);
		sureButton.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_sure_down));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					((ImageButton) v).setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.key_sure));
				}
				return false;
			}
		});
	}

	public void onClick(View v) {

		Intent gameIntent = null;
		switch (v.getId()) {
		case R.id.sureButton:
			if( checkInput() == false ) {
				break;
			}
			Util.sign = 1;
			gameIntent = new Intent(this,LoginActivity.class);
			this.startActivity(gameIntent);
			this.finish();
			break;
		default:
			break;
		}
	}
	
	private boolean checkInput() {
		
		String ip1s = ip1.getText().toString();
		String ip2s = ip2.getText().toString();
		String ip3s = ip3.getText().toString();
		String ip4s = ip4.getText().toString();
		String ports = port.getText().toString();
		
		if( ip1s.length() == 0 || ip2s.length() == 0 || ip3s.length() == 0 || ip4s.length() == 0 ) {
			Util.toastPopWindow(this,"请填写完成的ip地址");
			return false;
		}
		
		if( ports.length() == 0 ) {
			Util.toastPopWindow(this,"请填写端口号");
			return false;
		}
		
		Util.serverdata.setIP(ip1s + "." + ip2s + "." + ip3s + "." + ip4s);
		Util.serverdata.setPort(ports);
		
		writeServerData(Util.serverdbpath);
		
		return true;
	}
	
	private boolean readServerFile() {
		
		if( readServerData(Util.serverdbpath) == true ) {
			
			String[] ips = Util.serverdata.getIP().split(".");
			if( ips == null || ips.length != 4 ) {
				Util.serverdata.setIP("");
				Util.serverdata.setPort("");
				return false;
			}
			
			ip1.setText(ips[0]);
			ip2.setText(ips[1]);
			ip3.setText(ips[2]);
			ip4.setText(ips[3]);
			port.setText(Util.serverdata.getPort());
			
			return true;
		}
		return false;
	}
	
	private boolean writeServerData( String path ) {
		
		try {
			
			FileOutputStream fos = this.openFileOutput(path,Context.MODE_PRIVATE);
			
			byte[] ip = Util.serverdata.getIP().getBytes();
			byte[] len = new byte[1];
			len[0] = (byte) ip.length;
			fos.write(len);
			fos.write(ip);
			
			byte[] port = Util.serverdata.getPort().getBytes();
			len[0] = (byte) port.length;
			fos.write(len);
			fos.write(port);
			
			fos.close();
			
			return true;
		} catch (Exception e) {
            e.printStackTrace();
            return false;
        }
	}
	
	private boolean readServerData( String path ) {
		
		try {
			
			/*
			 * 判断文件存在需要全路径，这里使用try catch.
			 * */
			// path => data/data/com.ql.app/files/stockDic.dat
			// File file = new File(path);
			
			FileInputStream fin = openFileInput(path);
			
			byte[] len = new byte[1];
			if( fin.read(len) == -1 ) {
				return false;
			}
			byte[] ip = new byte[len[0]];
			if( fin.read(ip) == -1 ) {
				return false;
			}
			
			if( fin.read(len) == -1 ) {
				return false;
			}
			byte[] port = new byte[len[0]];
			if( fin.read(port) == -1 ) {
				return false;
			}
			fin.close();
			
			Util.serverdata.setIP(new String(ip));
			Util.serverdata.setPort(new String(port));
			
			return true;
			
		} catch (Exception e) {
            e.printStackTrace();
            return false;
        }
	}

}
